package cn.e3mall.controller;

import cn.e3mall.common.pojo.EasyUIDataGridResult;
import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.content.service.ContentCategoryService;
import cn.e3mall.content.service.ContentService;
import cn.e3mall.pojo.TbContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/content")
public class ContentController {

    @Autowired
    private ContentCategoryService contentCategoryService;
    @Autowired
    private ContentService contentService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public E3Result getContent(TbContent map) {
        E3Result e3Result = contentService.addContent( map );
        return e3Result;
    }
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public E3Result updatetContent(TbContent map) {
        E3Result e3Result = contentService.updateContent( map );
        return e3Result;
    }

    @RequestMapping(value = "/category/list", method = RequestMethod.GET)
    public List<EasyUITreeNode> getContent(@RequestParam(name = "id", defaultValue = "0") Long parentId) {
        List<EasyUITreeNode> contentCatList = contentCategoryService.getContentCatList( parentId );
        return contentCatList;
    }

    @RequestMapping(value = "/category/create", method = RequestMethod.POST)
    public String createNode(@RequestParam Map<String, String> map) {
        Long parentId = Long.valueOf( map.get( "parentId" ) );
        String name = map.get( "name" );
        String result = contentCategoryService.addContentCategory( parentId, name );
        return result;
    }

    @RequestMapping(value = "/category/update", method = RequestMethod.POST)
    public String updateNode(@RequestParam Map<String, String> map) {
        Long id = Long.valueOf( map.get( "id" ) );
        String name = map.get( "name" );
        String result = contentCategoryService.updateContentCategory( id, name );
        return result;
    }

    @RequestMapping(value = "/category/delete", method = RequestMethod.POST)
    public String deleteNode(@RequestParam Map<String, String> map) {
        Long id = Long.valueOf( map.get( "id" ) );
        String result = contentCategoryService.deleteContentCategory( id );
        return result;
    }
    @RequestMapping(value = "/query/list", method = RequestMethod.GET)
    public EasyUIDataGridResult contentList(@RequestParam Map<String, String> map) {
        Long categoryId = Long.valueOf( map.get( "categoryId" ) );
        Integer page = Integer.valueOf( map.get( "page" ) );
        Integer rows = Integer.valueOf( map.get( "rows" ) );
        EasyUIDataGridResult result = contentCategoryService.queryContentCategory(categoryId,page,rows );
        return result;
    }

}
